<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select @change="change" style="width: 120px" v-model="currentYear">
        <el-option v-for="item of yearList" :key="item" :label="item" :value="item">
          {{ item }}
        </el-option>
      </el-select>
      <el-select @change="change" style="width: 120px;margin-left:10px" v-model="currentMonth">
        <el-option v-for="item of 12" :key="item" :label="item" :value="item">
          {{ item }}
        </el-option>
      </el-select>
    </el-row>

    <el-calendar v-model="currentDate">
      <template #dateCell="{ data:{day},date }">
        <div class="date-content">
          <span class="text"> {{ day | getDay }}</span>
          <span v-show="isWeek(date)" class="rest">休</span>
        </div> 
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: 'WorkCalendar',
  props: {
    startDate: {
      type: Date,
      // 回调函数式的返回值
      default: () => new Date()
    }
  },
  filters: {
    getDay(value) {
      // value.split('-')将字符串转换成数组是这样 ['2023', '01', '04']
      // value.split('-')[2] 01 02 的日期
      const day = value.split('-')[2]
      /**
       * startsWith() 方法用来判断当前字符串是否以另外一个给定的子字符串开头，并根据判断结果返回 true 或 false。 
       * substring(indexStart[, indexEnd]) 方法返回一个字符串在开始索引到结束索引之间的一个子集，或从开始索引直到字符串的末尾的一个子集。
       */
      return day.startsWith('0') ? day.substring(1) : day
    }
  },
  data () {
    return {
      currentMonth: null, // 当前月份
      currentYear: null, // 当前年份
      currentDate: null,
      yearList: []
    }
  },
  created () {
    //    处理起始时间
    // 组件要求起始时间必须是 周一开始 以一个月为开始
    // 这里为什么要加 1 getMonth() 是 0 到 11 所以要加1
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentYear = this.startDate.getFullYear()
    this.yearList = Array.from(Array(11), (v, i) => this.currentYear + i - 5)
    this.change()
  },
  methods: {
    // 获取最新的日期
    change() {
      this.currentDate = new Date(`${this.currentYear}-${this.currentMonth}-1`) // 以当前月的1号为起始
    },
    // 是否是休息日
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style  scoped>
 /deep/ .el-calendar-day {
  height:  auto;
 }
 /deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 /deep/ .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 /deep/ .el-calendar__header {
   display: none
 }
</style>